﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SecureDongle;

namespace Run_Application
{

    enum SDCmd : ushort
    {
        SD_FIND = 1,			//Find SecureDongle
        SD_FIND_NEXT,		//Find next
        SD_OPEN,			//Open SecureDongle
        SD_CLOSE,			//Close SecureDongle
        SD_READ,			//Read SecureDongle
        SD_WRITE,			//Write SecureDongle
        SD_RANDOM,			//Generate random
        SD_SEED,			//Generate seed
        SD_READ_USERID = 10,	//Read UID
        SD_CHECK_MODULE = 12,	//Check Module
        SD_CALCULATE1 = 14,	//Calculate1
        SD_CALCULATE2,		//Calculate1
        SD_CALCULATE3,		//Calculate1
    };

    enum SDErrCode : uint
    {
        ERR_SUCCESS = 0,							//No error
        ERR_NO_PARALLEL_PORT = 0x80300001,		//(0x80300001)No parallel port
        ERR_NO_DRIVER,							//(0x80300002)No drive
        ERR_NO_DONGLE,							//(0x80300003)No SecureDongle
        ERR_INVALID_PASSWORD,					//(0x80300004)Invalid password
        ERR_INVALID_PASSWORD_OR_ID,				//(0x80300005)Invalid password or ID
        ERR_SETID,								//(0x80300006)Set id error
        ERR_INVALID_ADDR_OR_SIZE,				//(0x80300007)Invalid address or size
        ERR_UNKNOWN_COMMAND,					//(0x80300008)Unkown command
        ERR_NOTBELEVEL3,						//(0x80300009)Inner error
        ERR_READ,								//(0x8030000A)Read error
        ERR_WRITE,								//(0x8030000B)Write error
        ERR_RANDOM,								//(0x8030000C)Generate random error
        ERR_SEED,								//(0x8030000D)Generate seed error
        ERR_CALCULATE,							//(0x8030000E)Calculate error
        ERR_NO_OPEN,							//(0x8030000F)The SecureDongle is not opened
        ERR_OPEN_OVERFLOW,						//(0x80300010)Open SecureDongle too more(>16)
        ERR_NOMORE,								//(0x80300011)No more SecureDongle
        ERR_NEED_FIND,							//(0x80300012)Need Find before FindNext
        ERR_DECREASE,							//(0x80300013)Dcrease error
        ERR_AR_BADCOMMAND,						//(0x80300014)Band command
        ERR_AR_UNKNOWN_OPCODE,					//(0x80300015)Unkown op code
        ERR_AR_WRONGBEGIN,						//(0x80300016)There could not be constant in first instruction in arithmetic 
        ERR_AR_WRONG_END,						//(0x80300017)There could not be constant in last instruction in arithmetic 
        ERR_AR_VALUEOVERFLOW,					//(0x80300018)The constant in arithmetic overflow
        ERR_UNKNOWN = 0x8030ffff,					//(0x8030FFFF)Unkown error

        ERR_RECEIVE_NULL = 0x80300100,			//(0x80300100)Receive null
        ERR_PRNPORT_BUSY = 0x80300101				//(0x80300101)Parallel port busy

    };

    public class AXDongle
    {
        private ushort pass1 = 0x97DC;
        private ushort pass2 = 0xA19C;
        private ushort pass3 = 0x9227;
        private ushort pass4 = 0xDE28;

        public bool InitDongle()
        {
            byte[] buffer = new byte[1000];
            object obbuffer = new object();
            ushort handle = 0;
            ushort p1 = 0;
            ushort p2 = 0;
            ushort p3 = 0;
            ushort p4 = 0;
            uint lp1 = 0;
            uint lp2 = 0;

            //int iMaxSD = 0;            
            try
            {

                SecureDonglecom SD = new SecureDonglecom();
                
                uint[] uiarrSDID = new uint[32];
                p1 = pass1; p2 = pass2; p3 = pass3; p4 = pass4;
                obbuffer = (object)buffer;
                ulong ret = SD.SecureDongle((ushort)SDCmd.SD_FIND, ref handle, ref lp1, ref lp2, ref p1, ref p2, ref p3, ref p4, ref obbuffer); //Find SecureDongle
                if (0 != ret)
                {
                    return false;
                }
                //ulong ret1 = SD.SecureDongle((ushort)SDCmd.SD_CLOSE, ref handle, ref lp1, ref lp2, ref p1, ref p2, ref p3, ref p4, ref obbuffer);
                //if (0 != ret1)
                //{
                //    return false;
                //}
            }
            catch (Exception)
            {
                return false;
            }
            finally
            {
                
                
            }
            return true;
        }

        public byte[] GetData()
        {
            byte[] arr=new byte[1000];
            byte[] buffer = new byte[1000];
            object obbuffer = new object();
            ushort handle = 0;
            ushort p1 = 0;
            ushort p2 = 0;
            ushort p3 = 0;
            ushort p4 = 0;
            uint lp1 = 0;
            uint lp2 = 0;

            int iMaxSD = 0;
            SecureDonglecom SD = new SecureDonglecom();

            try
            {
                uint[] uiarrSDID = new uint[32];
                uint iCurrID;

                p1 = pass1; p2 = pass2; p3 = pass3; p4 = pass4;
                
                obbuffer = (object)buffer;
                ulong ret = SD.SecureDongle((ushort)SDCmd.SD_FIND, ref handle, ref lp1, ref lp2, ref p1, ref p2, ref p3, ref p4, ref obbuffer); //Find SecureDongle

                if (0 != ret)
                {
                    return null;
                }
                uiarrSDID[iMaxSD] = lp1;
                iMaxSD++;
                iCurrID = uiarrSDID[0];
                for (int n = 0; n < iMaxSD; n++)
                {
                    lp1 = uiarrSDID[n];

                    //Open SecureDongle
                    ret = SD.SecureDongle((ushort)SDCmd.SD_OPEN, ref handle, ref lp1, ref lp2, ref p1, ref p2, ref p3, ref p4, ref obbuffer);
                    if (0 != ret)
                    {
                        return null;
                    }


                    //Read SecureDongle
                    p1 = 0; p2 = 20;
                    ret = SD.SecureDongle((ushort)SDCmd.SD_READ, ref handle, ref lp1, ref lp2, ref p1, ref p2, ref p3, ref p4, ref obbuffer);
                    arr = (byte[])obbuffer;

                    if (0 != ret)
                    {
                        return null;
                    }

                    ulong ret1 = SD.SecureDongle((ushort)SDCmd.SD_CLOSE, ref handle, ref lp1, ref lp2, ref p1, ref p2, ref p3, ref p4, ref obbuffer);
                    if (0 != ret1)
                    {
                        return null;
                    }
                }
            }
            catch (Exception)
            {

                return null;
            }
            return arr;
        }

    }
}
